package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.ImageDao;
import com.bocloud.cmp.entity.resource.Image;
import com.bocloud.cmp.model.SimpleImageBean;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 镜像DAO接口实现类
 * 
 * @author dzg
 * @since 2016.09.13
 *
 */
@Repository("imageDao")
public class ImageDaoImpl extends JdbcGenericDao<Image, Long> implements ImageDao {

	@Override
	public Image query(Long id) throws Exception {
		String sql = "select * from image where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Image> list = this.list(Image.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<Image> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select * from image a where a.is_deleted = 0 and status != 'DELETED'";
		sql = SQLHelper.buildRawSql(sql, page, rows, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.list(Image.class, sql, paramMap);
	}

	@Override
	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name,a.image_id from image a where a.is_deleted = 0";
		sql = SQLHelper.buildSql(sql, 1, Integer.MAX_VALUE, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<Image> images = this.list(Image.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (Image image : images) {
			beans.add(new SimpleBean(image.getId(), image.getName(), image.getImageId()));
		}
		return beans;
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from image a where a.is_deleted = 0 and status != 'DELETED'";
		sql = SQLHelper.buildRawSql(sql, params, null, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update image set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	@Override
	public List<Image> list() throws Exception {
		String sql = "select * from image where is_deleted = 0";
		return this.list(Image.class, sql);
	}

	@Override
	public Image getByImageId(String imageId) throws Exception {
		String sql = "select * from image where is_deleted = 0 and image_id = :imageId";
		Map<String, Object> param = MapTools.simpleMap("imageId", imageId);
		List<Image> list = this.list(Image.class, sql, param);
		Image image = null;
		if (!list.isEmpty()) {
			image = list.get(0);
		}
		return image;
	}

	@Override
	public boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = "update image set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	@Override
	public List<Image> listByVendorId(Long vendorId) throws Exception {
		String sql = "select * from image where is_deleted = 0 and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.list(Image.class, sql, params);
	}

	@Override
	public List<SimpleImageBean> listName(Long id, Long region) throws Exception {
		String sql = "select distinct a.os_name from image a where a.is_deleted = 0 and a.vendor_id =:id and a.region =:region";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("region", region);
		List<Image> images = this.list(Image.class, sql, params);
		List<SimpleImageBean> beans = new ArrayList<>();
		for (Image image : images) {
			beans.add(new SimpleImageBean(image.getOsName(), null, null));
		}
		return beans;
	}

	@Override
	public List<SimpleImageBean> listVersion(Long id, Long region, String name) throws Exception {
		String sql = "select a.os_version, a.image_id from image a where a.is_deleted = 0 and a.vendor_id =:id and a.region =:region and a.os_name =:name";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("region", region);
		params.put("name", name);
		List<Image> images = this.list(Image.class, sql, params);
		List<SimpleImageBean> beans = new ArrayList<>();
		for (Image image : images) {
			beans.add(new SimpleImageBean(null, image.getOsVersion(), image.getImageId()));
		}
		return beans;
	}
}
